package com.java28.rbac.controller;

import com.java28.rbac.pojo.User;
import com.java28.rbac.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {

    @Autowired
    UserService userService;

    @RequestMapping("login")
    public String login(){
        return "login";
    }

    @RequestMapping("loginDo")
    @ResponseBody
    public Map<String,Object> loginDo(User user, HttpSession session){
        User loginUser = userService.login(user);
        Map<String,Object> map = new HashMap<String,Object>();
        if (loginUser != null){
            session.setAttribute("user",loginUser);
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }

    @RequestMapping("signOut")
    public String signOut(HttpSession session){
        session.invalidate();
        return "login";
    }

    @RequestMapping("main")
    public String main(){
        return "/WEB-INF/user/main";
    }

}
